<#include "/layout/layout.ftl">
<#import "/layout/macro.ftl" as macro>
<#import "/layout/select.ftl" as my/>
<#assign css>
<#--此ftl中用到的css-->
<link rel="stylesheet" href="/assets/vendor/layui-v2.3.0/css/layui.css">
<style>

    .layui-upload-img{width: 92px; height: 92px; margin: 0 10px 10px 0;}
</style>

</#assign>
<#assign js>
<#--此ftl中用到的js-->
<script src="/assets/vendor/layui-v2.3.0/layui.js"></script>
<script>
    layui.use('upload', function() {
        var $ = layui.jquery
                , upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#img1'
            , url: '/upload/uploadImg'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#up_icon').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code != 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("#icon").val(res.data);
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        var uploadInst2 = upload.render({
            elem: '#img2'
            , url: '/upload/uploadImg'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#up_preIcon').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code != 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("#preIcon").val(res.data);
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });

	$(".btn-submit").click(function () {
		// 校验表单合法
		var bootstrapValidator = $(".form-edit").data('bootstrapValidator');
		bootstrapValidator.validate();
		if(bootstrapValidator.isValid()){
			$.ajax({
				type: "POST",
				url: "/buyIcon/edit",
				data: $(".form-edit").serialize(),
				dataType: "JSON",
				success: function(res){
					layer.msg(res.message, {time: 2000
					}, function(){
						location.href="/buyIcon/index";
					});
				}
			});
		}else{
			return false;
		}
	});

	$('.form-edit').bootstrapValidator({
		message: '请输入有效值',
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		}
	});

</script>
</#assign>
<@layout title="编辑" active="buyIcon">
<!-- Content Header (Page header) -->
<section class="content-header">
	<h1>
		<#if isAdd>
            新增
        <#else>
            编辑
        </#if>
		<small>随意购图标设置详细信息</small>
	</h1>
	<ol class="breadcrumb">
		<li><a href="/"><i class="fa fa-home"></i> 管理后台</a></li>
		<li><a href="#"><i class="fa fa-list-ul"></i> 随意购图标设置管理</a></li>
		<li class="active"><i class="fa fa-edit"></i>
		    <#if isAdd>
                新增
            <#else>
                编辑
            </#if>
		</li>
	</ol>
</section>
<!-- Main content -->
<section class="content">
	<div class="row">
		<div class="col-md-12">
			<!-- Default box -->
			<div class="box  box-primary">
				<form class="form-horizontal form-edit" method="post" action="buyIcon/edit">
					<div class="box-body">
						<input type="hidden" id="id" name="id" value="${buyIcon.id!}">
						<input type="hidden" id="isAdd" name="isAdd" value="${isAdd?string('true','false')}">
						<div class="form-group">
							<label class="col-sm-2 control-label">类型名称：</label>
							<div class="col-sm-10">
								<input required placeholder="请输入类型名称" id="notes" name="notes" class="form-control" type="text" value="${buyIcon.notes!}" >
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">图标：</label>
							<div class="col-sm-10">
								<#--<input required placeholder="请输入图标" id="icon" name="icon" class="form-control" type="text" value="${buyIcon.icon!}" >-->
								<input type="hidden" name="icon" id="icon" value="${buyIcon.icon!}">
								<div class="layui-upload">
									<button type="button" class="layui-btn" id="img1">上传图片</button>
									<div class="layui-upload-list">
										<img class="layui-upload-img" id="up_icon" src="/${buyIcon.icon!}">
										<p id="demoText"></p>
									</div>
								</div>
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">选中图标：</label>
							<input type="hidden" name="preIcon" id="preIcon" value="${buyIcon.preIcon!}">
							<div class="col-sm-10">
                                <div class="layui-upload">
                                    <button type="button" class="layui-btn" id="img2">上传图片</button>
                                    <div class="layui-upload-list">
                                        <img class="layui-upload-img" id="up_preIcon" src="/${buyIcon.preIcon!}">
                                        <p id="demoText"></p>
                                    </div>
                                </div>
							</div>
						</div>

					<div class="box-footer">
						<button type="button" class="btn btn-default btn-back">返回</button>
						<button type="button" class="btn btn-info pull-right btn-submit">提交</button>
					</div>

				</form>
			</div>
			<!-- /.box -->
		</div>
	</div>
</section>
<!-- /.content -->
</@layout>
